using Consul.AspNetCore;
using StackExchange.Redis;
using Zhaoxi.MSACommerce.VerificationServer.Service;
using ZhaoXi.MSACommerce.Consul.ServiceRegistrantion;
using ZhaoXi.MSACommerce.Consul.ServiceDiscovery;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

var serviceCheck = builder.Configuration.GetSection
    ("ServiceCheck").Get<ServiceCheckConfiguration>();
serviceCheck ??= new ServiceCheckConfiguration();
builder.Services.AddConsul();
builder.Services.AddConsulService(serviceConfiguration =>
{
    serviceConfiguration.ServiceAddress = new Uri(builder.Configuration["urls"] ?? builder.Configuration["applicationUrl"]);
}, serviceCheck);
builder.Services.AddConsulDiscovery();

builder.Services.AddHealthChecks();
builder.Services.AddSingleton<IConnectionMultiplexer>(_ => 
    ConnectionMultiplexer.Connect(builder.Configuration.GetConnectionString("RedisConnection"))
);

builder.Services.AddScoped<ISmsService, SmsService>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseCors("AllowAny");
app.UseHealthChecks(serviceCheck.Path);

app.UseAuthorization();

app.MapControllers();

app.Run();
